let app = getApp()
const db = wx.cloud.database({
  env: app.globalData.CLOUD_ENV
})
const DBqiye = db.collection('db_qiye');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: [
      '前端开发',
      'java开发',
      '嵌入式开发',
      'php开发',
      'python开发',
      '网络工程开发'
    ],
    lists: [],
    num: 0,
    pwd: '0259',
    show: true
  },
  // 解屏
  getPwd(e) {
    // console.log(e.detail.value)
    let pwd2 = e.detail.value;
    if (pwd2.length == 4) {
      if (pwd2 == this.data.pwd) {
        wx.setStorageSync('pwd2', pwd2)
        wx.showToast({
          title: '恭喜您，验证通过！',
          icon: 'none'
        })
        this.setData({
          show: false
        })
      } else {
        wx.showToast({
          title: '验证码错误',
          icon: 'none'
        })
      }
    }

    if (pwd2.length > 4) {
      wx.showToast({
        title: '验证码错误',
        icon: 'none'
      })
    }
  },
  // 选择岗位
  chooseType(e) {
    let type = e.currentTarget.dataset.type;
    let index = e.currentTarget.dataset.index;
    this.getQiyeList(type)
    this.setData({
      num: index
    })
  },
  // 获取真题列表
  async getQiyeList(type) {

    if (type == undefined) return;

    let res = await DBqiye.where({
      type
    }).count()
    let count = res.total;
    let all = [];
    // wx.showLoading({
    //   title: '正在加载。。',
    // })
    for (let i = 0; i < count; i += 20) {
      await DBqiye.where({
          type
        }).skip(i).get()
        .then(
          content => {
            // console.log('1.0', content)
            let {
              data
            } = content;
            if (data.length != 0) {
              let arr = data.map(item => {
                let obj = {
                  ...item
                }
                return obj;
              })
              all = all.concat(arr)
            }
          }
        )
    }
    this.setData({
      lists: all
    })
    // console.log(all)
    // wx.hideLoading();
  },
  // 删除
  removeFn(e) {
    let _id = e.currentTarget.dataset.id;
    let _openid = wx.getStorageSync('openid')
    if (_openid != app.globalData.OPENID) {
      wx.showToast({
        title: '您没有权限删除。',
        icon: 'none'
      })
      return false;
    }
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除这套面试题吗?',
      complete: (res) => {
        if (res.cancel) {

        }
        if (res.confirm) {
          DBqiye.doc(_id).remove().then(
            content => {
              console.log('content', content)
              let {
                errMsg
              } = content;
              if (errMsg == "document.remove:ok") {
                let arr = that.data.lists;
                let newArr = arr.filter(item => {
                  if (item._id != _id) {
                    return item
                  }
                })
                that.setData({
                  lists: newArr
                })
              } else {
                wx.showToast({
                  title: '删除题目失败。',
                  icon: 'none'
                })
              }
            }
          )
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getQiyeList('前端开发')
    let pwd2 = wx.getStorageSync('pwd2')
    if (pwd2) {
      if (pwd2 == this.data.pwd) {
        this.setData({
          show: false
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})